package com.panopset.flywheel;

import java.io.StringWriter;

/**
 * <h5>p - Replace</h5>
 *
 * In this example, &quot;Joe&quot; will be replaced with &quot;Mary&quot;.
 *
 * <pre>
 *
 * ${&#064p name}Joe${&#064q}${&@064r name}Mary${&#064q}
 *
 * </pre>
 *
 * In this example, a prior replacement is undone.
 *
 * <pre>
 *
 * ${&@064r name}${name}${&#064q}
 *
 * </pre>
 *
 * @author Karl Dinwiddie
 *
 */
public class CommandReplace extends MatchableCommand implements UserMatchableCommand {

    public static String getShortHTMLText() {
        return "${&#064;r name}";
    }

    public CommandReplace(String source, String innerPiece, Template template) {
        super(source, innerPiece, template);
    }

    @Override
    public void resolve(StringWriter notUsed) {
        StringWriter sw = new StringWriter();
        super.resolve(sw);
        String toReplace = template.flywheel.get(getParams());
        template.flywheel.getReplacements().add(new String[] {
                com.panopset.Util.isPopulated(toReplace) ?
                        toReplace : getParams(), sw.toString()
        });
    }
}
